﻿using HK.Core.Utils;
using HK.Editor.GUIEx.Styles;
using HK.Editor.Utils;
using UnityEditor;
using UnityEngine;

namespace HK.Editor.Extend
{
    /// <summary>
    /// 编辑器类
    /// </summary>
    public class EditorBase : UnityEditor.Editor
    {
        /// <summary>
        /// 有效标志位
        /// </summary>
        protected bool Valid => null != target;

        /// <summary>
        /// 只读属性
        /// </summary>
        protected virtual bool ReadOnly => false;

        /// <summary>
        /// 脏字标志位
        /// </summary>
        protected bool Dirty { get; set; } = false;

        protected virtual void Awake()
        {
            // 初始化
            UtilsIo.Init();
        }

        protected virtual void OnEnable()
        {
            // 初始化各个序列化对象
            if (!InitSerializedProperties())
            {
                this.Warning("OnEnable():There is no serializedProperty named sData");
            }
        }

#region SerializedProperty

        /// <summary>
        /// 查找序列化对象
        /// </summary>
        /// <param name="iPropertyName">属性名</param>
        /// <returns>序列化对象</returns>
        protected SerializedProperty FindSerializedProperty(string iPropertyName)
        {
            if (!Valid) return null;
            var targetProperty = serializedObject.FindProperty(iPropertyName);
            if (null == targetProperty)
            {
                this.Error ("FindSerializedProperty():The property of {0} is not exist!!!",
                    iPropertyName);
                return null;
            }
            return targetProperty;
        }
        
        /// <summary>
        /// 查找序列化对象
        /// </summary>
        /// <param name="iProperty">属性</param>
        /// <param name="iSubProperty">子属性名</param>
        /// <returns>序列化对象</returns>
        protected SerializedProperty FindSerializedProperty(SerializedProperty iProperty, string iSubProperty)
        {
            if (!Valid || null == iProperty) return null;
            var targetProperty = iProperty.FindPropertyRelative(iSubProperty);
            if (null == targetProperty)
            {
                this.Error ("FindSerializedProperty():The property of {0} is not exist!!!",
                    iSubProperty);
                return null;
            }
            return targetProperty;
        }

        /// <summary>
        /// 初始化可序列化对象
        /// </summary>
        protected virtual bool InitSerializedProperties() => true;

#endregion

#region Draw

#region Background
        
        /// <summary>
        /// 绘制背景 - 一般
        /// </summary>
        /// <param name="iRect">绘制矩形区域</param>
        /// <param name="iEvent">Event</param>
        /// <returns>绘制的矩形区域</returns>
        protected Rect DrawGenericBg(Rect iRect, Event iEvent)
        {
            return UtilsGUIEx.DrawGenericBg(iRect, iEvent);
        }

        /// <summary>
        /// 绘制背景
        /// </summary>
        /// <param name="iRect">绘制矩形区域</param>
        /// <param name="iEvent">Event</param>
        /// <param name="iBgColor">背景色</param>
        /// <param name="iStyle">背景Style</param>
        /// <returns>绘制的矩形区域</returns>
        protected Rect DrawGenericBg(Rect iRect, Event iEvent, Color iBgColor, GUIStyle iStyle)
        {
            return UtilsGUIEx.DrawGenericBg(iRect, iEvent, iBgColor, iStyle, false, false);
        }
        
        /// <summary>
        /// 绘制背景 - 一般
        /// </summary>
        /// <param name="iRect">绘制矩形区域</param>
        /// <param name="iEvent">Event</param>
        /// <param name="iStyle">背景Style</param>
        /// <returns>绘制的矩形区域</returns>
        protected Rect DrawGenericBg(Rect iRect, Event iEvent, GUIStyle iStyle)
        {
            return DrawGenericBg(iRect, iEvent, Color.clear, iStyle);
        }
        
        /// <summary>
        /// 绘制背景 - 可点击
        /// </summary>
        /// <param name="iRect">绘制矩形区域</param>
        /// <param name="iEvent">Event</param>
        /// <returns>绘制的矩形区域</returns>
        protected Rect DrawClickableBg(Rect iRect, Event iEvent)
        {
            return DrawGenericBg(iRect, iEvent, UtilsGUIEx.ColorClickable, GuiExHelper.HeaderBackground);
        }
        
        /// <summary>
        /// 绘制背景 - 可拖拽
        /// </summary>
        /// <param name="iRect">绘制矩形区域</param>
        /// <param name="iEvent">Event</param>
        /// <returns>绘制的矩形区域</returns>
        protected Rect DrawDraggableBg(Rect iRect, Event iEvent)
        {
            return DrawGenericBg(iRect, iEvent, UtilsGUIEx.ColorDraggable, GuiExHelper.HeaderBackground);
        }

#endregion
        
#endregion
        
    }
}
